package operatemodel

import (
	"server/global"
	"server/model"
)


func Getallcate() (data []model.Category) {
	model.DB.Find(&data)
	return
}

func Addcate(name string) (code int) {
	var cate = model.Category{
		CategoryName: name,
	}
	d := model.DB.Create(&cate)
	if d.RowsAffected == 0 {
		code = global.REPEAT
		return
	}
	code = global.SUCCESS
	return
}

func Editcate(oldname string, newname string) (code int) {
	if d := model.DB.Model(&model.Category{}).Where("category_name=?", oldname).Update("category_name", newname); d.RowsAffected == 0 || d.Error != nil {
		code = global.FAIL
		return
	}
	code = global.SUCCESS
	return
}

func Deletecate(name string, id uint) (code int) {
	//Note:用原生sql语句名称要和数据库的名称一致（不区分大小写但是区分格式）才能操作成功
	if d := model.DB.Unscoped().Where("category_name=? and id=?", name, id).Delete(&model.Category{}); d.RowsAffected == 0 || d.Error != nil {
		code = global.FAIL
		return
	}
	code = global.SUCCESS
	return
}
